package com.fyzk.activity.special;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.fyzk.base.BaseActivity;
import com.fyzk.activity.app.AppToast;
import com.fyzk.bean.CoinDetailBean;
import com.fyzk.interfaces.MoreInterface;
import com.fyzk.interfaces.UpdateInterface;
import com.tyjc.lottery.R;
import com.fyzk.view.MsgListView;
import com.fyzk.view.TopTitleView;

import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import com.wf.domain.UrlDef;
import http.DataUtil.md5.MD5;

import static org.xutils.common.util.MD5.md5;

/**
 * Created by fanxs11 on 16/12/28.
 *
 * 积分详情
 */

@ContentView(R.layout.ac_coin_detail)
public class CoinDetailActivity extends BaseActivity implements MoreInterface, UpdateInterface {

    @ViewInject(R.id.coin_detail_top_rl)
    private TopTitleView topTitleView;

    @ViewInject(R.id.coin_detail_coin_tv)
    private TextView coin_tv;

    @ViewInject(R.id.coin_detail_tospend_btn)
    private Button toSpend;

    @ViewInject(R.id.coin_detail_all_tv)
    private TextView all;

    @ViewInject(R.id.coin_detail_income_tv)
    private TextView income;

    @ViewInject(R.id.coin_detail_spend_tv)
    private TextView spend;

    @ViewInject(R.id.coin_detail_list)
    private MsgListView listView;

    private List<CoinDetailBean.ValueBean.DataBean> infos = new ArrayList<>();
    private CoinDetailAdapter adapter;

    private int page = 1;
    private int count = 20;
    private boolean isUpdate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        topTitleView.setOnTitleClickListener(this);

        adapter = new CoinDetailAdapter(getContext(), infos);
        listView.setAdapter(adapter);
        listView.setUpdateInterface(this);
        listView.setMoreInterface(this);

        coinDefailRequest();
    }

    // 请求积分列表
    private void coinDefailRequest() {

        RequestParams params = new RequestParams(UrlDef.INTEGRAL_URL);

        String user_id = "16101300000002";
        String transid = MD5.toMd5((System.currentTimeMillis() + "").getBytes());
        String seckey = md5(md5(user_id+transid+ "apiclient"));

        params.addParameter("user_id", user_id);
        params.addParameter("transid", transid);
        params.addParameter("seckey", seckey);
        params.addParameter("page", page);
        params.addParameter("perpage", count);
        Log.d(TAG, "coinDefailRequest: 联网请求:"+params);

        x.http().get(params, new Callback.CacheCallback<JSONObject>() {
            @Override
            public void onSuccess(JSONObject result) {
                Log.d(TAG, "onSuccess: 联网返回成功json:"+result);
                CoinDetailBean bean = JSON.parseObject(result.toString(), CoinDetailBean.class);
                if (isUpdate) {
                    infos = bean.getValue().getData();
                    listView.onRefreshComplete();
                    isUpdate = false;
                } else {
                    infos.addAll(bean.getValue().getData());
                }

                if (page < bean.getValue().getTotalpage()) { // 显示加载更多
                    listView.listMore.endNet();
                    listView.listMore.setTextStr("获取更多");
                } else {
                    listView.listMore.setClickable(false);
                    listView.listMore.setTextStr("无更多数据");
                }
                adapter.setInfos(infos);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.d(TAG, "onError: 联网返回失败");
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }

            @Override
            public boolean onCache(JSONObject result) {
                return false;
            }
        });

    }

    @Override
    public void onRightClick() {
        AppToast.getToast().show("帮助");
    }

    @Override
    public void onClick(View arg0) {
        switch (arg0.getId()) {
            case R.id.coin_detail_tospend_btn:
                AppToast.getToast().show("去消费");

                break;
            case R.id.coin_detail_all_tv:
                AppToast.getToast().show("所有");

                break;
            case R.id.coin_detail_income_tv:
                AppToast.getToast().show("收入");

                break;
            case R.id.coin_detail_spend_tv:
                AppToast.getToast().show("支出");

                break;
            default:
                break;
        }
    }

    @Override
    public void more() {
        page++;
        coinDefailRequest();
    }

    @Override
    public void update() {
        isUpdate = true;
        page = 1;
        coinDefailRequest();
    }
}
